package com.foreveredu.erlms.web.util;

import java.io.OutputStream;
import java.util.List;

import com.foreveredu.erlms.domain.Enroll;
import com.linuxense.javadbf.DBFException;
import com.linuxense.javadbf.DBFField;
import com.linuxense.javadbf.DBFWriter;

public class DBFWriterTool {

	public static void buildDBF(List<Enroll> enrollList, OutputStream out)
			throws DBFException {

		// 创建DBF字段
		DBFField[] fields = new DBFField[20];

		// 设置字段名称、类型及长度 Character C java.lang.String/Numeric N java.lang.Double
		fields[0] = new DBFField();
		fields[0].setName("zkzh");// 准考证号
		fields[0].setDataType(DBFField.FIELD_TYPE_C);
		fields[0].setFieldLength(16);

		fields[1] = new DBFField();
		fields[1].setName("bmh");// 报名号
		fields[1].setDataType(DBFField.FIELD_TYPE_C);
		fields[1].setFieldLength(14);

		fields[2] = new DBFField();
		fields[2].setName("xm");// 姓名
		fields[2].setDataType(DBFField.FIELD_TYPE_C);
		fields[2].setFieldLength(40);

		fields[3] = new DBFField();
		fields[3].setName("xb");// 性别
		fields[3].setDataType(DBFField.FIELD_TYPE_C);
		fields[3].setFieldLength(1);

		fields[4] = new DBFField();
		fields[4].setName("csrq");// 出生日期
		fields[4].setDataType(DBFField.FIELD_TYPE_C);
		fields[4].setFieldLength(8);

		fields[5] = new DBFField();
		fields[5].setName("mz");// 民族
		fields[5].setDataType(DBFField.FIELD_TYPE_C);
		fields[5].setFieldLength(2);

		fields[6] = new DBFField();
		fields[6].setName("sfzh");// 身份证号码
		fields[6].setDataType(DBFField.FIELD_TYPE_C);
		fields[6].setFieldLength(18);

		fields[7] = new DBFField();
		fields[7].setName("zy");// 职业
		fields[7].setDataType(DBFField.FIELD_TYPE_C);
		fields[7].setFieldLength(2);

		fields[8] = new DBFField();
		fields[8].setName("whcd");// 文化程度
		fields[8].setDataType(DBFField.FIELD_TYPE_C);
		fields[8].setFieldLength(1);

		fields[9] = new DBFField();
		fields[9].setName("yzkzh");// 原准考证号
		fields[9].setDataType(DBFField.FIELD_TYPE_C);
		fields[9].setFieldLength(16);

		fields[10] = new DBFField();
		fields[10].setName("blcjzl");// 保留成绩种类
		fields[10].setDataType(DBFField.FIELD_TYPE_C);
		fields[10].setFieldLength(1);

		fields[11] = new DBFField();
		fields[11].setName("kspxzl");// 考试排序种类
		fields[11].setDataType(DBFField.FIELD_TYPE_C);
		fields[11].setFieldLength(1);

		fields[12] = new DBFField();
		fields[12].setName("lxdz");// 联系地址
		fields[12].setDataType(DBFField.FIELD_TYPE_C);
		fields[12].setFieldLength(32);

		fields[13] = new DBFField();
		fields[13].setName("lxdh");// 联系电话
		fields[13].setDataType(DBFField.FIELD_TYPE_C);
		fields[13].setFieldLength(32);

		fields[14] = new DBFField();
		fields[14].setName("dgcl");//
		fields[14].setDataType(DBFField.FIELD_TYPE_L);
		fields[14].setFieldLength(1);

		fields[15] = new DBFField();
		fields[15].setName("dgcp");//
		fields[15].setDataType(DBFField.FIELD_TYPE_L);
		fields[15].setFieldLength(1);

		fields[16] = new DBFField();
		fields[16].setName("havexp");//
		fields[16].setDataType(DBFField.FIELD_TYPE_N);
		fields[16].setFieldLength(11);

		fields[17] = new DBFField();
		fields[17].setName("sjkch");// 上机考场号
		fields[17].setDataType(DBFField.FIELD_TYPE_C);
		fields[17].setFieldLength(4);

		fields[18] = new DBFField();
		fields[18].setName("sjpc");// 上机排次
		fields[18].setDataType(DBFField.FIELD_TYPE_N);
		fields[18].setFieldLength(11);

		fields[19] = new DBFField();
		fields[19].setName("zdyxx");// 自定义信息
		fields[19].setDataType(DBFField.FIELD_TYPE_C);
		fields[19].setFieldLength(40);

		// 设置DBF字段
		DBFWriter writer = new DBFWriter();
		writer.setCharactersetName("GBK");
		writer.setFields(fields);

		// 定义行数据
		if (enrollList != null && enrollList.size() > 0) {
			Object[][] rowData = new Object[enrollList.size()][];

			for (int i = 0; i < rowData.length; i++) {
				Enroll enroll = enrollList.get(i);

				rowData[i] = new Object[20];
				rowData[i][0] = enroll.getExaminee().getEnrollNumber() == null ? ""
						: "00" + enroll.getExaminee().getEnrollNumber();
				rowData[i][1] = enroll.getExaminee().getEnrollNumber();
				rowData[i][2] = enroll.getName().trim();
				for (int j = 0; j < rowData[i][2].toString().trim().length(); j++) {
					String temp = rowData[i][2].toString();
					temp += " ";
					rowData[i][2] = temp;
				}

				rowData[i][3] = String.valueOf(enroll.getSex());
				rowData[i][4] = enroll.getBirthday();
				rowData[i][5] = enroll.getNation();
				rowData[i][6] = enroll.getExaminee().getIdCard();
				rowData[i][7] = enroll.getProfession();
				rowData[i][8] = enroll.getDegree();
				rowData[i][9] = enroll.getOldTicketNumber();
				rowData[i][10] = String.valueOf(enroll.getKeepGradeType());
				rowData[i][11] = "1";
				rowData[i][12] = enroll.getDept().getDeptName();
				for (int j = 0; j < rowData[i][12].toString().trim().length(); j++) {
					String temp = rowData[i][12].toString();
					temp += " ";
					rowData[i][12] = temp;
				}

				rowData[i][13] = enroll.getPhone();
				rowData[i][14] = false;
				rowData[i][15] = false;
				rowData[i][16] = 0.0;
				rowData[i][17] = "1";
				rowData[i][18] = 0.0;
				rowData[i][19] = enroll.getClazz() == null ? "" : enroll.getClazz().getClazzId();
				for (int j = 0; j < rowData[i][19].toString().trim().length(); j++) {
					String temp = rowData[i][19].toString();
					temp += " ";
					rowData[i][19] = temp;
				}

				// 将行数据写入到DBF中
				writer.addRecord(rowData[i]);
			}
		}

		// 将DBF输出
		writer.write(out);

	}

	//导出未交费学生数据表
	public static void buildNoPaidDBF(List<Enroll> enrollList, OutputStream out)
			throws DBFException {

		// 创建DBF字段
		DBFField[] fields = new DBFField[20];

		// 设置字段名称、类型及长度 Character C java.lang.String/Numeric N java.lang.Double
		fields[0] = new DBFField();
		fields[0].setName("wbh");// 网报号
		fields[0].setDataType(DBFField.FIELD_TYPE_C);
		fields[0].setFieldLength(16);

		fields[1] = new DBFField();
		fields[1].setName("kmdm");// 科目代码
		fields[1].setDataType(DBFField.FIELD_TYPE_C);
		fields[1].setFieldLength(14);

		fields[2] = new DBFField();
		fields[2].setName("xm");// 姓名
		fields[2].setDataType(DBFField.FIELD_TYPE_C);
		fields[2].setFieldLength(40);

		fields[3] = new DBFField();
		fields[3].setName("xb");// 性别
		fields[3].setDataType(DBFField.FIELD_TYPE_C);
		fields[3].setFieldLength(1);

		fields[4] = new DBFField();
		fields[4].setName("csrq");// 出生日期
		fields[4].setDataType(DBFField.FIELD_TYPE_C);
		fields[4].setFieldLength(8);

		fields[5] = new DBFField();
		fields[5].setName("mz");// 民族
		fields[5].setDataType(DBFField.FIELD_TYPE_C);
		fields[5].setFieldLength(2);

		fields[6] = new DBFField();
		fields[6].setName("sfzh");// 身份证号码
		fields[6].setDataType(DBFField.FIELD_TYPE_C);
		fields[6].setFieldLength(18);

		fields[7] = new DBFField();
		fields[7].setName("zy");// 职业
		fields[7].setDataType(DBFField.FIELD_TYPE_C);
		fields[7].setFieldLength(2);

		fields[8] = new DBFField();
		fields[8].setName("whcd");// 文化程度
		fields[8].setDataType(DBFField.FIELD_TYPE_C);
		fields[8].setFieldLength(1);

		fields[9] = new DBFField();
		fields[9].setName("yzkzh");// 原准考证号
		fields[9].setDataType(DBFField.FIELD_TYPE_C);
		fields[9].setFieldLength(16);

		fields[10] = new DBFField();
		fields[10].setName("blcjzl");// 保留成绩种类
		fields[10].setDataType(DBFField.FIELD_TYPE_C);
		fields[10].setFieldLength(1);

		fields[11] = new DBFField();
		fields[11].setName("kspxzl");// 考试排序种类
		fields[11].setDataType(DBFField.FIELD_TYPE_C);
		fields[11].setFieldLength(1);

		fields[12] = new DBFField();
		fields[12].setName("lxdz");// 联系地址
		fields[12].setDataType(DBFField.FIELD_TYPE_C);
		fields[12].setFieldLength(32);

		fields[13] = new DBFField();
		fields[13].setName("lxdh");// 联系电话
		fields[13].setDataType(DBFField.FIELD_TYPE_C);
		fields[13].setFieldLength(32);

		fields[14] = new DBFField();
		fields[14].setName("dgcl");//
		fields[14].setDataType(DBFField.FIELD_TYPE_L);
		fields[14].setFieldLength(1);

		fields[15] = new DBFField();
		fields[15].setName("dgcp");//
		fields[15].setDataType(DBFField.FIELD_TYPE_L);
		fields[15].setFieldLength(1);

		fields[16] = new DBFField();
		fields[16].setName("havexp");//
		fields[16].setDataType(DBFField.FIELD_TYPE_N);
		fields[16].setFieldLength(11);

		fields[17] = new DBFField();
		fields[17].setName("sjkch");// 上机考场号
		fields[17].setDataType(DBFField.FIELD_TYPE_C);
		fields[17].setFieldLength(4);

		fields[18] = new DBFField();
		fields[18].setName("sjpc");// 上机排次
		fields[18].setDataType(DBFField.FIELD_TYPE_N);
		fields[18].setFieldLength(11);

		fields[19] = new DBFField();
		fields[19].setName("clazzId");// 自定义信息
		fields[19].setDataType(DBFField.FIELD_TYPE_C);
		fields[19].setFieldLength(40);

		// 设置DBF字段
		DBFWriter writer = new DBFWriter();
		writer.setCharactersetName("GBK");
		writer.setFields(fields);

		// 定义行数据
		if (enrollList != null && enrollList.size() > 0) {
			Object[][] rowData = new Object[enrollList.size()][];

			for (int i = 0; i < rowData.length; i++) {
				Enroll enroll = enrollList.get(i);

				rowData[i] = new Object[20];
				rowData[i][0] = enroll.getExaminee().getSerialNumber();
				rowData[i][1] = enroll.getRank().getCode();
				rowData[i][2] = enroll.getName().trim();
				for (int j = 0; j < rowData[i][2].toString().trim().length(); j++) {
					String temp = rowData[i][2].toString();
					temp += " ";
					rowData[i][2] = temp;
				}

				rowData[i][3] = String.valueOf(enroll.getSex());
				rowData[i][4] = enroll.getBirthday();
				rowData[i][5] = enroll.getNation();
				rowData[i][6] = enroll.getExaminee().getIdCard();
				rowData[i][7] = enroll.getProfession();
				rowData[i][8] = enroll.getDegree();
				rowData[i][9] = enroll.getOldTicketNumber();
				rowData[i][10] = String.valueOf(enroll.getKeepGradeType());
				rowData[i][11] = "1";
				rowData[i][12] = enroll.getDept().getDeptName();
				for (int j = 0; j < rowData[i][12].toString().trim().length(); j++) {
					String temp = rowData[i][12].toString();
					temp += " ";
					rowData[i][12] = temp;
				}

				rowData[i][13] = enroll.getPhone();
				rowData[i][14] = false;
				rowData[i][15] = false;
				rowData[i][16] = 0.0;
				rowData[i][17] = "1";
				rowData[i][18] = 0.0;
				rowData[i][19] = enroll.getClazz() == null ? "" : enroll.getClazz().getClazzId();
				for (int j = 0; j < rowData[i][19].toString().trim().length(); j++) {
					String temp = rowData[i][19].toString();
					temp += " ";
					rowData[i][19] = temp;
				}

				// 将行数据写入到DBF中
				writer.addRecord(rowData[i]);
			}
		}

		// 将DBF输出
		writer.write(out);

	}
}
